package com.doctor.ui.office;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doctor.adapter.ShowImageAdapter;
import com.doctor.bean.OfficeBean;
import com.doctor.comm.ConstConfig;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.DragImageActivity;
import com.doctor.ui.R;
import com.doctor.view.NoScrollGridView;

public class LookInfoFrag extends BaseFragment{

	/**
	 * 查看信息
	 */
	
	private OfficeBean bean;
	private Context context;
	private TextView title;
	private TextView remindDate;
	private TextView remindTime;
	private TextView content;
	private LinearLayout backBtn;
	//显示图片之用
	private NoScrollGridView gridView;
	private ShowImageAdapter adapter;
	private List<String> beanList;
	private Handler handler;
	private LoadImages thread;
	private String table;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context=getActivity();
		bean=(OfficeBean) getArguments().getSerializable(ConstConfig.BEAN);
		table=getArguments().getString(ConstConfig.TABLE);
		handler=new Handler();
		thread=new LoadImages();
		
		View view=inflater.inflate(R.layout.look_info, container, false);
		initiaView(view);
		
		return view;
	}
	
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		title=(TextView) view.findViewById(R.id.title);
		remindDate=(TextView) view.findViewById(R.id.remind_date);
		remindTime=(TextView) view.findViewById(R.id.remind_time);
		content=(TextView) view.findViewById(R.id.content);
		backBtn=(LinearLayout) view.findViewById(R.id.backBtn);
		
		if (bean!=null) {
			title.setText(bean.getTitle());
			remindDate.setText(bean.getRemindDate());
			remindTime.setText(bean.getRemindTime());
			content.setText(bean.getDetail());
		}
		backBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getActivity().getSupportFragmentManager().popBackStack();
			}
		});
		gridView=(NoScrollGridView) view.findViewById(R.id.gridView);
		beanList=new ArrayList<String>();
		adapter=new ShowImageAdapter(context, beanList);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				String picPath=(String) arg1.getTag(R.id.bean_tag);
				Intent intent=new Intent(getActivity(),DragImageActivity.class);
				intent.putExtra(getString(R.string.uri), picPath);
				startActivity(intent);
			}
		});
		gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				//无实际作用，主要防止长按后响应onItemClick事件
				return true;
			}
		});
		handler.postDelayed(thread, DbOperator.delayTime);
	}
	
	/**
	 * 从数据库查询图片uri
	 * @param tableName
	 * @param id
	 */
	private void getImageFromDb(String tableName,int id){
		String images=DbOperator.getInstance(context).selectImages(tableName, id);
		if (images!=null && !"".equals(images)) {
			String[] imageArray= images.split(ConstConfig.IMAGE_SPLIT);
			for (String image : imageArray) {
				beanList.add(image);
			}
			adapter.notifyDataSetChanged();
		}
	}
	
	private class LoadImages extends Thread{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			getImageFromDb(table, bean.getId());
		}
	}
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		if (adapter != null) {
			adapter.clear();
		}
	}
	
}
